from sympy import Matrix, eye, zeros, ones

# 矩阵创建
A = Matrix([[1, 2], [3, 4]])
B = Matrix([[5, 6], [7, 8]])
print("矩阵A:", A) # 矩阵A: Matrix([[1, 2], [3, 4]])
print("矩阵B:", B) # 矩阵B: Matrix([[5, 6], [7, 8]])

# 矩阵运算
print("矩阵加法:", A + B) # 矩阵加法: Matrix([[6, 8], [10, 12]])
print("矩阵乘法:", A * B) # 矩阵乘法: Matrix([[19, 22], [43, 50]])
print("矩阵行列式:", A.det()) # 矩阵行列式: -2
print("矩阵逆:", A.inv()) # 矩阵逆: Matrix([[-2, 1], [3/2, -1/2]])
print("特征值:", A.eigenvals()) # 特征值: {5/2 - sqrt(33)/2: 1, 5/2 + sqrt(33)/2: 1}
print("特征向量:", A.eigenvects())

# 特殊矩阵
print("3阶单位矩阵:", eye(3))     # 3阶单位矩阵: Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
print("3×3零矩阵:", zeros(3, 3)) # 3×3零矩阵: Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
print("2×2全1矩阵:", ones(2, 2))  # 2×2全1矩阵: Matrix([[1, 1], [1, 1]])

# 线性系统求解
b = Matrix([1, 2]) 
solution = A.LUsolve(b)
print("线性系统解:", solution) # 线性系统解: Matrix([[0], [1/2]])